<!-- 批次线下的历年批次 -->
<template>
  <div>
    <!--  城市的下拉列表框  -->
    <!--    <div class="app-container">-->
    <!--      <nav>-->
    <!--        地区：-->
    <!--        <select v-model="cityId" class="city" @change="handleChange">-->
    <!--          <option v-for="item in cityList" :key="item.id" :value="item.id">{{ item.name }}</option>-->
    <!--        </select>-->
    <!--      </nav>-->
    <!--      <nav>-->
    <!--        考试科目类型：-->
    <!--        <select v-model="selectedArtsSciences" @change="handleArtsSciencesChange">-->
    <!--          <option v-for="(item, index) in artsSciencesList" :key="index" :value="item">{{ item }}</option>-->
    <!--        </select>-->
    <!--      </nav>-->
    <!--    </div>-->

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" label-position="left"
             style="padding-left: 20px;">
      <p></p>
      <el-form-item label="地区" prop="cityId">
        <el-select v-model="cityId" placeholder="请选择地区" class="city" @change="handleChange">
          <el-option v-for="item in cityList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="考试类型" prop="selectedArtsSciences">
        <el-select v-model="artsSciences" placeholder="请选择考试科目类型" @change="handleArtsSciencesChange">
          <el-option v-for="(item, index) in artsSciencesList" :key="index" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>

      <!--      <el-form-item label="地区id" prop="areaId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.areaId"-->
      <!--          placeholder="请输入地区id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="批次年份" prop="year">
        <el-date-picker clearable
                        v-model="queryParams.year"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择批次年份">
        </el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="批次id" prop="batchId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.batchId"-->
      <!--          placeholder="请输入批次id"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="考试科目类型" prop="artsSciencesId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.artsSciencesId"-->
      <!--          placeholder="请输入考试科目类型"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--   文理类型   -->
      <!--      <el-form-item label="文理类型名字" prop="artsSciences">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.artsSciences"-->
      <!--          placeholder="请输入文理类型名字"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="批次线" prop="score">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.score"-->
      <!--          placeholder="请输入批次线"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="批次排名" prop="order">
        <el-input v-model="queryParams.order" placeholder="请输入批次排名" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <!--    同步批次id做下拉列表    -->
      <el-form-item label="批次" prop="batchName">
        <el-select v-model="batchName" placeholder="请选择批次" @change="batchIdChange">
          <el-option v-for="(item, index) in batchNameList" :key="index" :label="item.volunteerBatch"
                     :value="item.volunteerBatch"></el-option>
        </el-select>
      </el-form-item>

      <!--      <el-form-item label="保留字段" prop="seq">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.seq"-->
      <!--          placeholder="请输入保留字段"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="上传时间" prop="uploadTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.uploadTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择上传时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="是否是普通的批次类型" prop="isNormal">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.isNormal"-->
      <!--          placeholder="请输入是否是普通的批次类型"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="是否有效" prop="flag">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.flag"-->
      <!--          placeholder="请输入是否有效"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <br/>
      <div style="display: block;">
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8" style="margin-left: 20px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:data:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:data:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:data:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:data:export']"
        >导出
        </el-button>
      </el-col>
      <div style="padding-right: 20px;">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </div>
    </el-row>

    <div style="padding-right: 20px; padding-left: 20px;">
      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" fixed="left"/>
        <!--      <el-table-column label="历年批次招生数据的id" align="center" prop="previousYearsBatchEnrollmentDataId"/>-->
        <el-table-column label="序号" align="center" prop="num" fixed="left" width="80px"/>
        <el-table-column label="地区" align="center" prop="city" width="80px"/>
        <el-table-column label="批次年份" align="center" prop="year" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.year, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="批次" align="center" prop="batchName" width="200px"/>
        <el-table-column label="考试科目类型" align="center" prop="artsSciencesId" width="130px"/>
        <el-table-column label="文理类型名字" align="center" prop="artsSciences" width="130px"/>
        <el-table-column label="批次线" align="center" prop="score" width="120px"/>
        <el-table-column label="批次排名" align="center" prop="order" width="120px"/>
        <!--      <el-table-column label="保留字段" align="center" prop="seq"/>-->
        <el-table-column label="上传时间" align="center" prop="uploadTime" width="220px">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.uploadTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="批次类型(是否普通)" align="center" prop="isNormal" width="150px"/>

        <!--      <el-table-column label="是否有效" align="center" prop="flag"/>-->

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:data:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:data:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改历年批次招生数据（vf_previous_years_batch_enrollment_data）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <!--        <el-form-item label="地区id" prop="areaId">-->
        <!--          <el-input v-model="form.areaId" placeholder="请输入地区id"/>-->
        <!--        </el-form-item>-->

        <!--    地区改下拉列表框    -->
        <el-form-item label="地区" prop="areaId">
          <el-select v-model="form.areaId" placeholder="请选择地区">
            <el-option v-for="item in cityList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="批次年份" prop="year">
          <el-date-picker clearable
                          v-model="form.year"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择批次年份">
          </el-date-picker>
        </el-form-item>
        <!--    批次id改批次的下拉列表框    -->
        <el-form-item label="批次" prop="batchId">
          <el-select v-model="form.batchId" placeholder="请选择批次">
            <el-option v-for="(item, index) in batchNameList" :key="index" :label="item.volunteerBatch"
                       :value="item.piCiId"></el-option>
          </el-select>
        </el-form-item>

        <!--        <el-form-item label="批次id" prop="batchId">-->
        <!--          <el-input v-model="form.batchId" placeholder="请输入批次id"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="考试科目类型" prop="artsSciencesId">
          <el-select v-model="form.artsSciencesId" placeholder="请选择考试科目类型">
            <el-option label="文科" value="112"></el-option>
            <el-option label="理科" value="114"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文理类型名字" prop="artsSciences">
          <el-input v-model="form.artsSciences" placeholder="请输入文理类型名字"/>
        </el-form-item>
        <el-form-item label="批次线" prop="score">
          <el-input v-model="form.score" placeholder="请输入批次线"/>
        </el-form-item>
        <el-form-item label="批次排名" prop="order">
          <el-input v-model="form.order" placeholder="请输入批次排名"/>
        </el-form-item>
        <!--        <el-form-item label="保留字段" prop="seq">-->
        <!--          <el-input v-model="form.seq" placeholder="请输入保留字段"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="上传时间" prop="uploadTime">
          <el-date-picker clearable
                          v-model="form.uploadTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择上传时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否是普通的批次类型" prop="isNormal">
          <el-select v-model="form.isNormal" placeholder="请选择是否是普通的批次类型">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="是否有效" prop="flag">-->
        <!--          <el-input v-model="form.flag" placeholder="请输入是否有效"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addData,
  delData,
  getAllBatchName,
  getArtsSciences,
  getCity,
  getData,
  listData,
  updateData
} from "@/api/system/data";

export default {
  name: "Data",
  data() {
    return {
      // 所有支援你看批次名称
      batchNameList: [],
      batchName: '',
      batchId: '',
      // 考试科目类型
      artsSciences: '',
      // 城市
      city: '',
      cityId: '',
      cityList: [],
      // 文理类型名字
      artsSciencesList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历年批次招生数据（vf_previous_years_batch_enrollment_data）表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaId: null,
        year: null,
        batchId: null,
        artsSciencesId: null,
        artsSciences: null,
        score: null,
        order: null,
        seq: null,
        uploadTime: null,
        isNormal: null,
        flag: 0
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaId: [
          {required: true, message: "地区id不能为空", trigger: "blur"}
        ],
        year: [
          {required: true, message: "批次年份不能为空", trigger: "blur"}
        ],
        batchId: [
          {required: true, message: "批次id不能为空", trigger: "blur"}
        ],
        artsSciencesId: [
          {required: true, message: "考试科目类型不能为空", trigger: "blur"}
        ],
        artsSciences: [
          {required: true, message: "文理类型名字不能为空", trigger: "blur"}
        ],
        score: [
          {required: true, message: "批次线不能为空", trigger: "blur"}
        ],
        order: [
          {required: true, message: "批次排名不能为空", trigger: "blur"}
        ],
        seq: [
          {required: true, message: "保留字段不能为空", trigger: "blur"}
        ],
        uploadTime: [
          {required: true, message: "上传时间不能为空", trigger: "blur"}
        ],
        isNormal: [
          {required: true, message: "是否是普通的批次类型不能为空", trigger: "blur"}
        ],
        flag: [
          {required: true, message: "是否有效不能为空", trigger: "blur"}
        ]
      }
    };
  },
  beforeMount() {
    // 获取所有志愿批次名
    getAllBatchName()
      .then(response => {
        this.batchNameList = response;
        console.log("所有志愿批次名:");
        console.log(this.batchNameList);
      }).catch(error => {
      console.error('获取历年批次的所有文理类型数据失败:', error);
    });
    // 获取城市列表
    getCity(2)
      .then(response => {
        this.cityList = response;
      }).catch(error => {
      console.error('获取地区数据失败:', error);
    });
    // 获取历年批次的所有文理类型（不重复）
    getArtsSciences()
      .then(response => {
        this.artsSciencesList = response;
        console.log("获取历年批次的所有文理类型");
        console.log(this.artsSciencesList);
      }).catch(error => {
      console.error('获取历年批次的所有文理类型数据失败:', error);
    });
  },
  created() {
    this.getList();
  },
  methods: {
    // 根据批次名获取id
    batchIdChange(event) {
      for (let i = 0; i < this.batchNameList.length; i++) {
        if (this.batchNameList[i].volunteerBatch == event) {
          console.log("比对成功");
          this.queryParams.batchId = this.batchNameList[i].piCiId;
          i = this.batchNameList.length;
        }
      }
    },
    // 同步获取城市id
    handleChange(event) {
      // 当下拉列表框的值发生变化时，更新cityId为选中城市的ID
      console.log("城市change");
      console.log(event);
      this.cityId = event;
    },
    handleArtsSciencesChange(event) {
      console.log("科目类型");
      console.log(event);
      this.artsSciences = event;
    },
    /** 查询历年批次招生数据（vf_previous_years_batch_enrollment_data）列表 */
    getList() {
      this.loading = true;
      listData(this.queryParams).then(response => {
        // 给response添加序号
        for (let i = 0; i < response.rows.length; i++) {
          response.rows[i].num = i + 1;
        }
        // 遍历 response.rows 数组，为每个元素添加 city 字段
        if (this.cityList.length == 0) {
          for (let i = 0; i < response.rows.length; i++) {
            response.rows[i].city = "北京";
          }
        } else {
          response.rows.forEach(row => {
            // 这里假设 cityId 与城市列表中的 ID 对应，如果不是，需要根据实际情况获取对应的城市名称
            row.city = this.cityList.find(city => city.id === row.areaId)?.name || '';
          });
        }

        // 遍历修改考试科目类型
        for (let i = 0; i < response.rows.length; i++) {
          if (response.rows[i].artsSciencesId == 114) {
            response.rows[i].artsSciencesId = "理科";
          } else {
            response.rows[i].artsSciencesId = "文科";
          }
        }
        // 遍历修改是否是普通的批次类型，0是1不是
        for (let i = 0; i < response.rows.length; i++) {
          if (response.rows[i].isNormal == 0) {
            response.rows[i].isNormal = "是";
          } else {
            response.rows[i].isNormal = "否";
          }
        }
        // 遍历所有志愿批次名匹配id给response添加志愿名
        if (this.batchNameList.length == 0) {
          this.getAllBatchName();
        }
        for (let i = 0; i < this.batchNameList.length; i++) {
          for (let j = 0; j < response.rows.length; j++) {
            // 遍历判断id是否一致，如果一致则添加志愿名
            if (response.rows[j].batchId == this.batchNameList[i].piCiId) {
              response.rows[j].batchName = this.batchNameList[i].volunteerBatch;
              // 执行后跳过

            }
          }
        }
        console.log("默认数据");
        console.log(response);
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        previousYearsBatchEnrollmentDataId: null,
        areaId: null,
        year: null,
        batchId: null,
        artsSciencesId: null,
        artsSciences: null,
        score: null,
        order: null,
        seq: null,
        uploadTime: null,
        isNormal: null,
        flag: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // 城市id
      this.queryParams.areaId = this.cityId;
      // 考试科目类型
      this.queryParams.artsSciences = this.artsSciences;
      console.log("查询参数：");
      console.log(this.queryParams);
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.previousYearsBatchEnrollmentDataId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      // 截取year，只要年份
      this.form.year = this.form.year.substring(0, 4);
      this.title = "添加历年批次招生数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const previousYearsBatchEnrollmentDataId = row.previousYearsBatchEnrollmentDataId || this.ids
      getData(previousYearsBatchEnrollmentDataId).then(response => {
        this.form = response.data;
        this.open = true;
        // 截取year，只要年份
        this.form.year = this.form.year.substring(0, 4);
        this.title = "修改历年批次招生数据";
        console.log("修改的数据：");
        console.log(response);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 年份转换（2025-05-01）只要年份
          this.form.year = this.form.year.substring(0, 4);
          if (this.form.previousYearsBatchEnrollmentDataId != null) {
            updateData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
        console.log("提交的对象：");
        console.log(this.form)
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const previousYearsBatchEnrollmentDataIds = row.previousYearsBatchEnrollmentDataId || this.ids;
      this.$modal.confirm('是否确认删除历年批次招生数据编号为"' + previousYearsBatchEnrollmentDataIds + '"的数据项？').then(function () {
        return delData(previousYearsBatchEnrollmentDataIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/data/export', {
        ...this.queryParams
      }, `data_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
